package com.yksj.healthtalk.ui.messageleave;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.DoctorMessageAdapter;
import com.yksj.healthtalk.adapter.DoctorMessageAdapter.OnclickReplyListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.CommonExplainActivity;
import com.yksj.healthtalk.entity.LeaveMessage;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 查询个人的所有留言
 * @author Administrator
 *
 */
public class DoctorMessageLookSingleClient extends BaseFragmentActivity implements OnClickListener, OnclickReplyListener, OnRefreshListener<ListView>{
	
	private String messageId;//回复留言时  留言的id
	private DoctorMessageAdapter adapter;
	private TextView mText;//日期text显示
	private PullToRefreshListView mPullListView;
	private ListView mListView;
	private List<LeaveMessage> list=new ArrayList<LeaveMessage>();
	private int loadNum=0;
	private String date="";
	private String  messageCoumeID;//对方的id
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_message_look_single_all);
		messageCoumeID = ((LeaveMessage) getIntent().getSerializableExtra("entity")).getCUSTOMER_ID();
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("留言管理");
		titleTextV.setText("公告板");
		findViewById(R.id.calender).setOnClickListener(this);
		
		
		mText = (TextView) findViewById(R.id.date_text);
		mText.setOnClickListener(this);
		findViewById(R.id.calender_button).setOnClickListener(this);
		mPullListView = (PullToRefreshListView)findViewById(R.id.hall);
		mPullListView.setOnRefreshListener(this);
		mListView = mPullListView.getRefreshableView();
		adapter = new DoctorMessageAdapter(list,this,1);
		mListView.setAdapter(adapter);
		adapter.setOnclickReplyListener(this);
		initData();
	}
	
	

	private void initData(){
		HttpRestClient.doHttpDoctorMessageLookSingleAll(loadNum+"",messageCoumeID,date,new ObjectHttpResponseHandler(this) {
			@Override
			public List<LeaveMessage> onParseResponse(String content) {
				return adapter.onparseJson(content);
			}
			
			@Override
			public void onSuccess(Object response) {
				super.onSuccess(response);
				if(response==null)return;
				adapter.mList.addAll((Collection<? extends LeaveMessage>) response);
				adapter.notifyDataSetChanged();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mPullListView.onRefreshComplete();
			}
		});
	}


	@Override
	public void onClick(View v) {
		Intent intent;
			switch (v.getId()) {
			case R.id.title_back:
				setResult(RESULT_OK, getIntent());
				finish();
				overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
				break;
			case R.id.title_right2:
				intent = new Intent(this,DoctorMessageBoardManager.class);
				intent.putExtra("customerid", messageCoumeID);
				startActivityForResult(intent,1000);
				break;
			case R.id.calender:
			case R.id.date_text:
			case R.id.calender_button:
				intent=new Intent(this,ChoiseDateActivity.class);
				if(mText.getText().toString().length()<5){
					intent.putExtra("date", TimeUtil.getMoneyTime());
				}else{
					intent.putExtra("date", mText.getText().toString());
				}
				startActivityForResult(intent,0);
				break;
			}
		}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return ;
		switch (requestCode) {
		case 0:
			if(data!=null){
				Bundle extras = data.getExtras();
				if(extras.containsKey("all")){
					date="";
					adapter.mList.clear();
					loadNum=0;
					mText.setText("全部");
				}else{
					if(extras.containsKey("nothing"))return;
					String string = extras.getString("date");
					String string1 = extras.getString("year");
					mText.setText(string1+"-"+string);
					date=string1+string;
					loadNum=0;
					adapter.mList.clear();
					}
			}else{
					date="";
					mText.setText(TimeUtil.getMoneyTime().substring(0,4)+"");
			}
			initData();
			break;
		case 2000:
			if(data!=null&&data.hasExtra("content")){
				String content=data.getStringExtra("content");
				if(content==null||"".equals(content))return;
				HttpRestClient.doHttpRepatLeaveMessage(content, messageId, new AsyncHttpResponseHandler(this){
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						try {
							JSONObject object=new JSONObject(content);
							ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("info"));
							if(object.optString("code").equals("0")){
								loadNum=0;
								adapter.mList.clear();
								initData();
							}
						} catch (Exception e) {
						}
					}
				});
			}
			break;
		}
	}
	
	@Override
	public void Onclick(LeaveMessage entity) {
		messageId=entity.getMESSAGE_ID();
		Intent intent=new Intent(this,CommonExplainActivity.class);
		intent.putExtra("title", "回复");
		intent.putExtra("message_leave", "11"); 
		intent.putExtra("mContentCount", getIntent().getIntExtra("mContentCount", 800)); 
		startActivityForResult(intent, 2000);
		
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadNum++;
		initData();
	}
}
